package com.zj.syzs.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.zj.shouyou.R;
import com.zj.syzs.adapter.GiftFmPageAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class GiftFragment extends Fragment implements ViewPager.OnPageChangeListener, RadioGroup.OnCheckedChangeListener {
    private ViewPager giftViewpager;
    private PagerAdapter pagerAdapter;
    private List<Fragment> list = new ArrayList<>();
    private RadioGroup radioGroup;

    private GiftFmPageAdapter gfmAdapter;


    public GiftFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_gift, container, false);
        initData();
        initView(view);
        return view;
    }

    private void initData() {
        list.add(new MobGameFragment());
        list.add(new WebGameFragment());
    }

    private void initView(View view) {
        //初始化礼包页面顶部RadioGroup
        radioGroup = (RadioGroup) view.findViewById(R.id.fragment_gift_rgs);
        radioGroup.setOnCheckedChangeListener(this);

        //初始化ViewPager
        giftViewpager = (ViewPager) view.findViewById(R.id.fragment_gift_viewpager);
        gfmAdapter = new GiftFmPageAdapter(getChildFragmentManager(), list);
        giftViewpager.setAdapter(gfmAdapter);
        giftViewpager.setOnPageChangeListener(this);
    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        RadioButton rb = (RadioButton) radioGroup.getChildAt(position);
        rb.setChecked(true);

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {

        switch (checkedId) {
            case R.id.fragment_gift_mobile_rb:
                giftViewpager.setCurrentItem(0);
                break;
            case R.id.fragment_gift_web_rb:
                giftViewpager.setCurrentItem(1);
                break;
        }
    }


}
